﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace UM_SERVER.Packet.InPacket
{
    /// <summary>
    ///  *   用户登陆包
    ///  *   基本包数据结构
    ///  *   用户名 
    ///  *   用户密码 (MD5加密)
    ///  *   登录状态
    /// </summary>
    public class LoginPacket : BaseInPackage
    {
        public LoginPacket(InPackage inpackage)
            : base(inpackage)
        {
        }

        public override void InitPackage()
        {
            int Offset = 0;
            byte[] UsernameLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, UsernameLength, 0, 4);
            Offset += 4;
            //读取用户名
            byte[] Username = new byte[BitConverter.ToInt32(UsernameLength, 0)];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Username, 0, Username.Length);
            Offset += Username.Length;
            //得到密码长度
            byte[] PasswordLength = new byte[4];
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, PasswordLength, 0, 4);
            Offset += 4;
            //得到密码
            byte[] Password = new byte[BitConverter.ToInt32(PasswordLength, 0)];
            //读取密码
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, Password, 0, Password.Length);
            Offset += Password.Length;
            byte[] LoginState = new byte[4];
            //读取登录状态
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, LoginState, 0, 4);
            Offset += 6;
            byte[] IPLength = new byte[4];
            //读取IP地址长度
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPLength, 0, 4);
            Offset += 4;
            byte[] IPAddress = new byte[BitConverter.ToInt32(IPLength, 0)];
            //读取IP地址
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, IPAddress, 0, IPAddress.Length);
            Offset += IPAddress.Length;
            byte[] TalkPort = new byte[4];
            //读取聊天端口
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, TalkPort, 0, 4);
            Offset += 4;
            byte[] VideoPort = new byte[4];
            //读取视频端口
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, VideoPort, 0, 4);
            Offset += 4;
            byte[] VoicePort = new byte[4];
            //读取语音端口
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, VoicePort, 0, 4);
            Offset += 4;
            byte[] TransferFilePort = new byte[4];
            //读取语音端口
            Array.ConstrainedCopy(this.Body["HEAD"], Offset, TransferFilePort, 0, 4);

            //开始打入集合
            this.DIC_CONTENT.Add("USERNAME", Username);
            this.DIC_CONTENT.Add("PASSWORD", Password);
            this.DIC_CONTENT.Add("LOGINSTATE", LoginState);
            this.DIC_CONTENT.Add("IPADDRESS", IPAddress);
            this.DIC_CONTENT.Add("TALKPORT", TalkPort);
            this.DIC_CONTENT.Add("VIDEOPORT", VideoPort);
            this.DIC_CONTENT.Add("VOICEPORT", VoicePort);
            this.DIC_CONTENT.Add("TRANSFER_FILE_PORT", TransferFilePort); 

        }

        /// <summary>
        ///     得到登陆密码
        /// </summary>
        /// <returns>
        ///     返回用户密码
        /// </returns>
        public String GetPassword()
        {
            return Encoding.Default.GetString(this.DIC_CONTENT["PASSWORD"]);
        }
        /// <summary>
        ///     得到登录状态
        /// </summary>
        /// <returns>
        ///     返回用户登录状态
        /// </returns>
        public LoginState GetLoginState()
        {
            return (LoginState)BitConverter.ToInt32(this.DIC_CONTENT["LOGINSTATE"], 0);
        }
        /// <summary>
        ///     得到视频端口
        /// </summary>
        /// <returns>
        ///     返回视频端口
        /// </returns>
        public int GetVideoPort()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["VIDEOPORT"], 0);
        }
        /// <summary>
        ///     得到语音端口
        /// </summary>
        /// <returns>
        ///     返回语音端口
        /// </returns>
        public int GetVoicePort()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["VOICEPORT"], 0);
        }

        /// <summary>
        ///     得到文件传送端口
        /// </summary>
        /// <returns>
        ///     返回文件传送端口
        /// </returns>
        public int GetTransferFilePort()
        {
            return BitConverter.ToInt32(this.DIC_CONTENT["TRANSFER_FILE_PORT"], 0);
        }

        public override byte[] EncryptBody()
        {
            throw new NotImplementedException();
        }

        public override byte[] DecryptBody()
        {
            throw new NotImplementedException();
        }

        public override int GetcryptograshStart()
        {
            throw new NotImplementedException();
        }

        /// <summary>
        ///     登录状态
        /// </summary>
        public enum LoginState
        {
            /// <summary>
            ///     在线
            /// </summary>
            Normal,
            /// <summary>
            ///     隐身
            /// </summary>
            Hidden,
            /// <summary>
            ///     忙碌
            /// </summary>
            Busy,
            /// <summary>
            ///     离开
            /// </summary>
            Away
        }

    }
}
